package com.cpi.cornpp.futurepurchase.action;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.customerpurchase.service.CustomerPurchaseService;
import com.cpi.cornpp.futurepurchase.form.SearchFuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseSearchCriteriaView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cpi.cornpp.period.service.PeriodService;

public class ShowFuturePurchaseQueryAction extends Action {
	
	private static final String SUCCESS_FORWARD = "toSuccess";	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int currentYear = 0;
		FuturePurchaseService futurePurchaseService = null;
		FuturePurchaseSearchCriteriaView purchaseSearchCriteriaView = null;
		CustomerPurchaseService customerPurchaseService = null;		
		PeriodService periodService = null;
		List futurePurchases = null;
		List customerPurchases = null;
		List periodYears = null;
		List periods = null;
		SearchFuturePurchaseForm searchFuturePurchaseForm = null;
		
		
		ActionForward forward = mapping.findForward(SUCCESS_FORWARD);
		HttpSession httpSession = null;
		httpSession = request.getSession(false);
		
		futurePurchaseService = new FuturePurchaseService();
		customerPurchaseService = new CustomerPurchaseService();
		periodService = new PeriodService();
		searchFuturePurchaseForm = (SearchFuturePurchaseForm)form;
		
		periods = periodService.getActivePeriods();		
		currentYear = Calendar.getInstance().get(Calendar.YEAR);
		purchaseSearchCriteriaView = new FuturePurchaseSearchCriteriaView();
		
		purchaseSearchCriteriaView.setBeginPeriodYear(currentYear);
		purchaseSearchCriteriaView.setEndPeriodYear(currentYear+1);
		
		futurePurchases = futurePurchaseService.getFuturePurchasesByPeriodYearRange(purchaseSearchCriteriaView, searchFuturePurchaseForm.getOrderField(),(1 == searchFuturePurchaseForm.getOrderType()) ? false: true);
		
		if (!CollectionUtils.isEmpty(futurePurchases)){
			Iterator iterator = futurePurchases.iterator();
			FuturePurchaseView futurePurchaseView = null;
			while( iterator.hasNext() ){
				futurePurchaseView = (FuturePurchaseView)iterator.next();
				customerPurchases = customerPurchaseService.getCustomerPurchasesByFuturePurchase(futurePurchaseView.getFuturePurchaseId());
				
				if (!CollectionUtils.isEmpty(customerPurchases)){
					futurePurchaseView.setCustomerPurchases(customerPurchases);
				}
			}
		}
				
		periodYears = periodService.getYears();
		
		request.setAttribute("periods", periods);
		request.setAttribute("periodYears", periodYears);
		request.setAttribute("bPeriodYear", new Integer( currentYear ));
		request.setAttribute("ePeriodYear", new Integer( currentYear+1));
		request.setAttribute("futurePurchases", futurePurchases);
		
		httpSession.setAttribute("previousOrderField", Integer.toString(searchFuturePurchaseForm.getOrderField()));
		httpSession.setAttribute("previousOrderType", Integer.toString(searchFuturePurchaseForm.getOrderType()));
		
		forward = mapping.findForward(SUCCESS_FORWARD);
		
		return forward;
	}
}